package alarm.ruiqi.com.alarm_pro.options.head;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ab.util.AbViewUtil;

import alarm.ruiqi.com.alarm_pro.R;
import alarm.ruiqi.com.alarm_pro.wigdet.DrawerArrowDrawable;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link GeneralHeadFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 */
public class GeneralHeadFragment extends Fragment {

    private OnFragmentInteractionListener mListener;
    private DrawerArrowDrawable mDrawerArrowDrawable;
    private View mParentView;
    private TextView mHeadText;
    private View mAddedRightView;

    public GeneralHeadFragment() {
    }
    public DrawerArrowDrawable getmDrawerArrowDrawable() {
        return mDrawerArrowDrawable;
    }

    //region 生命周期
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mParentView != null) {
            ViewGroup parent = (ViewGroup) mParentView.getParent();
            if (parent != null) parent.removeView(mParentView);
            return mParentView;
        } else {
            mParentView = inflater.inflate(R.layout.fragment_general_head, container, false);
        }

        ImageView main_drawer_indicator = (ImageView) mParentView.findViewById(R.id.main_drawer_indicator);
        mDrawerArrowDrawable = new DrawerArrowDrawable(getResources());
        mDrawerArrowDrawable.setStrokeColor(getResources().getColor(R.color.tomato));
        main_drawer_indicator.setImageDrawable(mDrawerArrowDrawable);
        main_drawer_indicator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onDrawerIndicatorChange(v);
            }
        });
        mHeadText = (TextView) mParentView.findViewById(R.id.main_drawer_text);
        return mParentView;
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }
    //endregion

    //region 右侧图标相关
    //添加右侧图标和点击事件
    public void addRightDrawable(int drawableId, View.OnClickListener onClickListener) {
        ImageView imageView = (ImageView) mParentView.findViewById(R.id.main_drawer_right_indicator);
        imageView.setImageResource(drawableId);
        imageView.setOnClickListener(onClickListener);
    }
    //添加右侧图标和点击事件
    public void togglrRightDrawable(boolean flag) {
        ImageView imageView = (ImageView) mParentView.findViewById(R.id.main_drawer_right_indicator);
        imageView.setVisibility(flag ? View.VISIBLE : View.GONE);
    }
    //endregion

    //region 自定义右侧view相关
    public void addRightView(View  v) {
        mAddedRightView = v;
        RelativeLayout rl = (RelativeLayout) mParentView;
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        lp.addRule(RelativeLayout.CENTER_VERTICAL);
        lp.addRule(RelativeLayout.LEFT_OF,mHeadText.getId());
        v.setPadding(0,0, (int)AbViewUtil.dip2px(getActivity(),10),0);
        v.setLayoutParams(lp);
        rl.addView(v);
    }

    public void removeRightView() {
        RelativeLayout rl = (RelativeLayout) mParentView;
        rl.removeView(mAddedRightView);
    }

    public View getmAddedRightView() {
        return mAddedRightView;
    }

    public void setmAddedRightView(View mAddedRightView) {
        this.mAddedRightView = mAddedRightView;
    }
    //endregion

    //region 文本设置
    public  void  setHeadText(String text){
        mHeadText.setText(text);
    }
    public  void  setHeadText(@StringRes int resId){
        mHeadText.setText(resId);
    }
    //endregion


    //region 交互与 实现的接口

    public void setOnFragmentInteractionListener(OnFragmentInteractionListener mListener) {
        this.mListener = mListener;
    }

    public interface OnFragmentInteractionListener {
        //public void onFragmentInteraction(Uri uri);
        void onDrawerIndicatorChange(View v);
    }
    //endregion

}
